/**
 * 产品分类列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;

    var data = { "code":0, "msg":"", "count":json.totalCount };

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        defaultToolbar: ['filter']
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    var ids = new Array();

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        let items = obj.config.data;
        ids = zmnPage.layIdsChecked(items, "categId");
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一选择一条数据！");
        }

        zmnPage.layStartStop(obj, "/serv/showCateg/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if (obj.data.showSort != null && obj.data.showSort != "" && obj.field != 'showSimpleSpell'){
            zmnPage.laySortPositiveInteger(obj.value,"/serv/showCateg/updateSort.action",obj.data.categId,10000,1000);
        }

        if (obj.data.showSimpleSpell != null && obj.data.showSimpleSpell != "" && obj.field != "showSort"){
            zmnPage.laySimpleSpell(obj.value,"/serv/showCateg/updateShowSimpleSpell.action",obj.data.categId,3,1000);

        }
    });

    form.on('switch(chk_status)', function(data){
        console.log(data.elem); //得到checkbox原始DOM对象
        console.log(data.elem.checked); //开关是否开启，true或者false
        let obj = new Object();
        if (data.elem.value == 2){
            obj.event = 'stop';
        } else{
            obj.event = 'start';
        }
        zmnPage.layStartStop2(obj, "/serv/categ/updateStatus.action", data.elem.name);
        console.log(data.value); //开关value值，也可以通过data.elem.value得到
        console.log(data.othis); //得到美化后的DOM对象
    });

    form.on("switch(chk_show_status)", function (obj) {
        console.log(obj);
        let  url = "/serv/showCateg/updateStatus.action";
        let categId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        console.log(status);
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: categId, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: categId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    //对外暴露的接口
    exports('art_serv_categ_show_list', {});
});

/**
 * 描述: 修改前台分类名称
 **/
function modifyCategoryName(categId){
    zmnLayUtil.openIframeCustom(layer, "/serv/showCateg/detail.action?categId="+categId, {title: "修改前台分类名称", shade: 0.8, area:['550px', '220px']});
}

/**
 * 预览图片
 * @param element
 */
function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = document.getElementById(element).src;
    // img.src = obj.src;
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px',300+'px'],
        shadeClose:true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}

//显示描述
function show_tips(flag, tips, obj){
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips,obj,{
            tips:[1,'#3595CC'],
            time:3000
        })
    }
}
